package com.fish.anne.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.fish.anne.service.OrderPaymentCfgManager;
import com.fish.anne.model.OrderPaymentCfg;
import com.fish.anne.webapp.action.BaseAction;

import java.util.List;

public class OrderPaymentCfgAction extends BaseAction implements Preparable {
    private OrderPaymentCfgManager orderPaymentCfgManager;
    private List orderPaymentCfgs;
    private OrderPaymentCfg orderPaymentCfg;
    private Long  pcId;

    public void setOrderPaymentCfgManager(OrderPaymentCfgManager orderPaymentCfgManager) {
        this.orderPaymentCfgManager = orderPaymentCfgManager;
    }

    public List getOrderPaymentCfgs() {
        return orderPaymentCfgs;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String orderPaymentCfgId = getRequest().getParameter("orderPaymentCfg.pcId");
            if (orderPaymentCfgId != null && !orderPaymentCfgId.equals("")) {
                orderPaymentCfg = orderPaymentCfgManager.get(new Long(orderPaymentCfgId));
            }
        }
    }

    public String list() {
        orderPaymentCfgs = orderPaymentCfgManager.getAll();
        return SUCCESS;
    }

    public void setPcId(Long  pcId) {
        this. pcId =  pcId;
    }

    public OrderPaymentCfg getOrderPaymentCfg() {
        return orderPaymentCfg;
    }

    public void setOrderPaymentCfg(OrderPaymentCfg orderPaymentCfg) {
        this.orderPaymentCfg = orderPaymentCfg;
    }

    public String delete() {
        orderPaymentCfgManager.remove(orderPaymentCfg.getPcId());
        saveMessage(getText("orderPaymentCfg.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (pcId != null) {
            orderPaymentCfg = orderPaymentCfgManager.get(pcId);
        } else {
            orderPaymentCfg = new OrderPaymentCfg();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (orderPaymentCfg.getPcId() == null);

        orderPaymentCfgManager.save(orderPaymentCfg);

        String key = (isNew) ? "orderPaymentCfg.added" : "orderPaymentCfg.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}